package com.msos.dao.BManagerDao;

import com.msos.model.All_Business.All_Business;
import com.msos.model.Message.Message;
import com.msos.model.business.Business;
import com.msos.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName: BManagerDao
 * @Description: TODO
 * @Author: hsx
 * @Date: 2022/4/15 19:34
 * @Version: v1.0
 */
public class BManagerDao {
    QueryRunner queryRunner = new QueryRunner(DBUtils.getDs());

    public List<All_Business> getAllBusiness(int start, int size, String sort, String order) throws SQLException {
        return queryRunner.query("select * from all_business order by "+sort+" "+order+" limit "+start+","+size,new BeanListHandler<>(All_Business.class));
    }

    public Long getTotal() throws SQLException {
        return queryRunner.query("select count(*) from all_business",new ScalarHandler<>());
    }

    public Integer addBusiness(String bname, String bintro, String bimg, String bmoney) throws SQLException {
        return queryRunner.update("insert into all_business (bname,bintro,bimg,bmoney) values (?,?,?,?)",bname,bintro,bimg,bmoney);
    }

    public All_Business getBusinessByName(String bname) throws SQLException {
        return queryRunner.query("select * from all_business where bname=?",new BeanHandler<>(All_Business.class),bname);
    }

    public void deleteBusinessByAid(int aid) throws SQLException {
        queryRunner.update("delete from all_business where aid=?",aid);
    }

    public Integer openBusinessByAid(String bname, String bintro, String bimg, Integer bmoney) throws SQLException {
        return queryRunner.update("insert into business (bname,bintro,bimg,bmoney) values (?,?,?,?)",bname,bintro,bimg,bmoney);
    }

    public All_Business getBusinessByAid(String aid) throws SQLException {
        return queryRunner.query("select * from all_business where aid=?",new BeanHandler<>(All_Business.class),Integer.parseInt(aid));
    }

    public Integer updateOpen(String aid,String open) throws SQLException {
        return queryRunner.update("update all_business set open=? where aid=?",open,Integer.parseInt(aid));
    }

    public Integer updateClose(String aid,String close) throws SQLException {
        return queryRunner.update("update all_business set close=? where aid=?",close,Integer.parseInt(aid));
    }

    public Integer closeBusinessByAid(String bname) throws SQLException {
        return queryRunner.update("delete from business where bname=?",bname);
    }

    public Integer updateBusiness(All_Business all_business) throws SQLException {
        return queryRunner.update("update all_business set bname=?,bmoney=?,bintro=?,bimg=? where aid=?",all_business.getBname(),all_business.getBmoney(),all_business.getBintro(),all_business.getBimg(),all_business.getAid());
    }
}
